<template>
  <div class="app-container">
    <div v-if="mode==1">
      <el-card class="operate-container" shadow="never">
        <i class="el-icon-tickets"></i>
        <span>请选择需要删除的套餐</span>
        <el-button size="mini" class="btn-add" @click="handleDeleteBind()" v-if="mode == 1">确定</el-button>
      </el-card>
      <div class="table-container">
        <el-table
                ref="newTable"
                :data="meallist"
                style="width: 100%;"
                @selection-change="handleSelectionMealChange"
                v-loading="listLoading1"
                border
        >
          <el-table-column type="selection" width="60" align="center"></el-table-column>
          <el-table-column label="编号" width="120" align="center">
            <template slot-scope="scope">{{scope.row.id}}</template>
          </el-table-column>
          <el-table-column label="套餐名称" width="250" align="center">
            <template slot-scope="scope">{{scope.row.mealName}}</template>
          </el-table-column>
          <el-table-column label="时长" width="250" align="center">
            <template slot-scope="scope">{{scope.row.duration}}小时</template>
          </el-table-column>
          <el-table-column label="价格" width="250" align="center">
            <template slot-scope="scope">{{scope.row.price}}元</template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span v-if="mode == 0">数据列表</span>
      <span v-if="mode == 1">请选择需要绑定的套餐</span>
      <el-button size="mini" class="btn-add" @click="handleget()">获取美团套餐</el-button>
      <el-button size="mini" class="btn-add" @click="handleAdd()" v-if="mode == 0">添加设备套餐</el-button>
      <el-button size="mini" class="btn-add" @click="handleBind()" v-if="mode == 1">确定</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="deviceTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="120" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="套餐名称" width="250" align="center">
            <template slot-scope="scope">{{scope.row.mealName}}</template>
        </el-table-column>
        <el-table-column label="时长" width="200" align="center">
          <template slot-scope="scope">{{scope.row.duration}}小时</template>
      </el-table-column>
        <el-table-column label="价格" width="200" align="center">
          <template slot-scope="scope">{{scope.row.price}}元</template>
        </el-table-column>
        <el-table-column label="美团套餐id" align="center">
          <template slot-scope="scope">{{scope.row.meituanDealId}}</template>
        </el-table-column>
        <el-table-column label="操作" width="240" align="center" v-if="mode==0">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdate(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { fetchList, deleteDevicemeal,listByDevice,getmeituandeal } from "@/api/qsg/devicemeal";
import { updateDevice,deleteBind } from "@/api/qsg/device";
import { formatDate } from "@/utils/date";
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10
};
export default {
  name: "deviceList",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      listQuery1:{},
      list: null,
      meallist:null,
      total: null,
      listLoading: false,
      listLoading1:false,
      multipleSelection: [],
      mode:0, //默认列表进来,1为设备挑选套餐进来
      device:{},
      device1:{},
      multipleSelectionMeal:[],
      meituandeals:[]
    };
  },
  created() {
    let deviceId = this.$route.query.id;
    if (typeof deviceId != 'undefined'){
      this.mode = 1;
      this.device.id = deviceId;
      this.listQuery1.deviceId = deviceId;
      this.listByDevice();
    }
    this.getList();
  },
  filters: {
    formatTime(time) {
      if (time == null || time === "") {
        return "N/A";
      }
      var n = time.replace("-","/");
      n = n.replace("-","/");
      let date = new Date(n);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
    formatSale(isSale) {
      if (isSale==0) {
        return "否";
      }else {
        return "是";
      }
    },
    formatXufei(isXufei) {
      if (isXufei==0) {
        return "否";
      }else {
        return "是";
      }
    }
  },
  methods: {
    listByDevice(){
      this.listLoading1 = true;
      listByDevice(this.listQuery1).then(response => {
        this.listLoading1 = false;
        this.meallist = response.data;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection)
    },
    handleSelectionMealChange(val) {
      this.multipleSelectionMeal = val;
      console.log(this.multipleSelectionMeal)
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleDelete(index, row) {
      this.$confirm("请确认该套餐没有正在进行的订单后再确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteDevicemeal(row.id).then(response => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    },
    handleAdd() {
      this.$router.push({ path: "/drivermanage/addDevicemeal" });
    },
    handleget(){
      this.$router.push({ path: "/drivermanage/meituan" });
    },
    handleBind() {
      let ids = '';
      for (let i=0; i<this.multipleSelection.length; i++) {
        ids = ids + this.multipleSelection[i].id + ','
      }
      ids = ids.substring(0,ids.lastIndexOf(","))
      this.device.mealId = ids;
      updateDevice(this.device.id, this.device).then(
              (response) => {
                this.$message({
                  message: "绑定成功",
                  type: "success",
                  duration: 1000,
                });
                this.$router.back();
              }
      );
      console.log(ids)
    },
    handleDeleteBind() {
      let ids = '';
      for (let i=0; i<this.multipleSelectionMeal.length; i++) {
        ids = ids + this.multipleSelectionMeal[i].id + ','
      }
      ids = ids.substring(0,ids.lastIndexOf(","))
      this.device1.mealId = ids;
      this.device1.id = this.device.id
      deleteBind(this.device1).then(
              (response) => {
                this.$message({
                  message: "解除绑定成功",
                  type: "success",
                  duration: 1000,
                });
                this.listByDevice();
              }
      );
      console.log(ids)
    },
    handleUpdate(index, row) {
      this.$router.push({
        path: "/drivermanage/updateDevicemeal",
        query: { id: row.id }
      });
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.records;
        this.total = response.data.total;
      });
    }
  }
};
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>


